package sjtu.ist.warehouse4ws.dao;

import java.util.List;

import org.hibernate.Query;

import sjtu.ist.warehouse4ws.model.Privilege;
import sjtu.ist.warehouse4ws.tool.ListPage;

public class PrivilegeDaoHibernate extends BaseDaoHibernate implements PrivilegeDao{

	@Override
	public Privilege get(String id) {
		return (Privilege)getHibernateTemplate().get(Privilege.class, id);
	}

	@Override
	public void save(Privilege privilege) {
		getHibernateTemplate().save(privilege);
	}

	@Override
	public void update(Privilege privilege) {
		getHibernateTemplate().update(privilege);
		
	}

	@Override
	public void delete(String id) {
		getHibernateTemplate().delete(get(id));
		
	}

	@Override
	public void delete(Privilege privilege) {
		getHibernateTemplate().delete(privilege);
		
	}

	@Override
	public ListPage getAllPrivileges(int start,int limit) {
		Query query=getSession().createQuery("from Privilege");
		query.setFirstResult(start);//定义从第几条开始查询   
        query.setMaxResults(limit);//定义返回的记录数
        List<Privilege> list=query.list();
		long totalCount = ((Long) getHibernateTemplate().find("select COUNT(*) from Privilege").get(0)).longValue(); 
		ListPage lp=new ListPage(list,(int)totalCount);
		return lp;
	}

	@Override
	public List<Privilege> findByCompany(String companyId) {
		return (List<Privilege>)getHibernateTemplate().find("from Privilege p where p.company='"+companyId+"'");
	}

	@Override
	public List<Privilege> findByFileInfo(String fileInfoId) {
		return (List<Privilege>)getHibernateTemplate().find("from Privilege p where p.fileInfoId='"+fileInfoId+"'");
	}

	@Override
	public ListPage searchPrivilege(String fileName, String project,
			String version, Integer type, String department, String company, String owner,int start,int limit) {
		// TODO Auto-generated method stub
		String select="select p ";
		String hql="from Privilege AS p ,Fileinfo AS f where p.fileInfoId=f.id";
		if(fileName!=null && !fileName.equalsIgnoreCase("")){
			hql=hql.concat(" and f.name like '%"+fileName+"%'");
		}
		if(project!=null && !project.equalsIgnoreCase("")){
			hql=hql.concat(" and f.project like '%"+project+"%'");
		}
		if(version!=null && !version.equalsIgnoreCase("")){
			hql=hql.concat(" and f.version like '%"+version+"%'");
		}
		if(type!=null){
			hql=hql.concat(" and f.type = "+type);
		}
		if(department!=null && !department.equalsIgnoreCase("")){
			hql=hql.concat(" and f.department = '"+department+"'");
		}
		if(company!=null && !company.equalsIgnoreCase("")){
			hql=hql.concat(" and p.company = '"+company+"'");
		}
		if(owner!=null && !owner.equalsIgnoreCase("")){
			hql=hql.concat(" and f.company = '"+owner+"'");
		}
		
		Query query=getSession().createQuery(select+hql);
		query.setFirstResult(start);//定义从第几条开始查询   
        query.setMaxResults(limit);//定义返回的记录数
        List<Privilege> list=query.list();
        
        long totalCount = ((Long) getHibernateTemplate().find("select COUNT(*) "+hql).get(0)).longValue(); 
        ListPage pl=new ListPage(list,(int)totalCount);
        
        
		//return (List<Privilege>)getHibernateTemplate().find(hql);
        return pl;
	}

	@Override
	public Privilege findByOp(String fileInfoId, String companyId) {
		// TODO Auto-generated method stub
		List<Privilege> p = (List<Privilege>)getHibernateTemplate().find("from Privilege p where p.fileInfoId='"+fileInfoId+"' and p.company = '"+companyId+"'");
		return p.get(0);
	}

}
